package com.nmsl.statistic.controller;

import com.nmsl.common.core.controller.BaseController;
import com.nmsl.common.core.domain.AjaxResult;
import com.nmsl.common.core.page.TableDataInfo;
import com.nmsl.statistic.domain.RetailMedicine;
import com.nmsl.statistic.mapper.RetailMedicineMapper;
import com.nmsl.statistic.vo.McslfMedicDetails;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/retailmedic")
public class RetailMedicController extends BaseController {
    @Autowired
    RetailMedicineMapper retailMedicineMapper;

    @RequestMapping("/list")
    public AjaxResult getList(){
        McslfMedicDetails mcslfMedicDetails = new McslfMedicDetails();
        startPage();

        List<McslfMedicDetails> mcslfMedicDetails1 = retailMedicineMapper.selectRetailMedicineListDetails(mcslfMedicDetails);
        Map<String,Object> map = new HashMap<>();
        map.put("data",mcslfMedicDetails1);
//计算单条记录采购总价 以及 单个利润
        List<McslfMedicDetails> collect = mcslfMedicDetails1.stream().map(item -> {
            //    采购总价  计算   purchasePrice*medicineNum
            BigDecimal bigDecimal = new BigDecimal(item.getMedicineNum());
            BigDecimal bigDecimal1 = new BigDecimal(item.getPurchasePrice());
            BigDecimal multiply = bigDecimal.multiply(bigDecimal1);
            item.setPurchasePriceAmunod(multiply);
            //    利润  计算 medicineAmunod-purchasePriceAmunod
            BigDecimal subtract = item.getMedicineAmunod().subtract(multiply);
            item.setPrefit(subtract);
            return item;
        }).collect(Collectors.toList());

        //采购总价
        BigDecimal purchasePriceAmunod = new BigDecimal(0);
//        零售总价
        BigDecimal retailPriceAmunod = new BigDecimal(0);
//        总利润
        BigDecimal fitAmunod = new BigDecimal(0);
        for (McslfMedicDetails item : collect) {
//            采购总价
            purchasePriceAmunod =  purchasePriceAmunod.add(item.getPurchasePriceAmunod());
            retailPriceAmunod = retailPriceAmunod.add(item.getMedicineAmunod());
        }
        fitAmunod = retailPriceAmunod.subtract(purchasePriceAmunod);
        map.put("purchasePriceAmunod",purchasePriceAmunod);
        map.put("retailPriceAmunod",retailPriceAmunod);
        map.put("fitAmunod",fitAmunod);
        map.put("data",collect);
        return AjaxResult.success(map);
    }
}
